
/**  
* @Title: FileUtil.java
* @Package com.utils.file
* @Description: 
* @author likai
* @date 2016年9月1日
* @version V1.0  
*/

package com.utils.file;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @ClassName: FileUtil
 * @Description: 文件生成、上传工具类
 * @author likai
 * @date 2016年9月1日
 *
 */

public class FileUtil {

	/**
	 * 文件上传
	 * 
	 * @param fis文件流，上传本地路径path
	 * @return
	 * @throws IOException
	 */
	public static boolean uploadFile(InputStream fis, String path) throws IOException {
		File file = new File(path);
		if (!file.getParentFile().exists()) {
			//文件夹不存在，创建文件夹
			file.getParentFile().mkdirs();
		}
		if (file.exists()) {
			file.delete();
		}
		file.createNewFile();
		FileOutputStream fos = new FileOutputStream(path);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		fis.close();
		fos.close();
		fis = null;
		fos = null;
		return true;
	}

	/**
	 * 生成文件
	 * 
	 * @param filepath本地上传路径，文本内容
	 * @return
	 * @throws IOException
	 */
	public static File createFile(String filepath, String content) throws IOException {
		File file = new File(filepath);
		if (file.exists()) {
			file.delete();
		}
		file.createNewFile();
		FileOutputStream out = new FileOutputStream(filepath);
		OutputStreamWriter oStreamWriter = new OutputStreamWriter(out, "utf-8");
		BufferedWriter bufferedWriter = new BufferedWriter(oStreamWriter);
		bufferedWriter.write(content);
		// 关闭流
		bufferedWriter.close();
		out.close();
		oStreamWriter.close();
		return file;

	}

	/**
	 * 从网络Url中下载文件
	 * 
	 * @param urlPath:网路文件路径
	 * @param fileName：本地保存的文件名
	 * @param savePath：本地保存的文件夹路径
	 * @throws IOException
	 */
	public static File downLoadWebFile(String urlPath, String savePath, String fileName) throws IOException {
		URL url = new URL(urlPath);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 设置超时间为3秒
		conn.setConnectTimeout(3 * 1000);
		// 防止屏蔽程序抓取而返回403错误
		conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

		// 得到输入流
		InputStream inputStream = conn.getInputStream();
		// 获取自己数组
		byte[] getData = readInputStream(inputStream);
		// 文件保存位置
		File saveDir = new File(savePath);
		if (!saveDir.exists()) {
			saveDir.mkdirs();
		}
		File file = new File(saveDir + File.separator + fileName);
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(getData);
		if (fos != null) {
			fos.close();
		}
		if (inputStream != null) {
			inputStream.close();
		}

		return file;
	}

	/**
	 * 从输入流中获取字节数组
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static byte[] readInputStream(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		bos.close();
		return bos.toByteArray();
	}

	public static void main(String[] args) {
		try {
			downLoadWebFile("http://h.hiphotos.baidu.com/image/pic/item/c995d143ad4bd113dc5cb7f853afa40f4afb0562.jpg",
					"E:/Downloads/","1.jpg");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
